<%@page import="com.stwitinc.Util"%>
<%@page import="com.alibaba.fastjson.JSONArray"%>
<%@page import="com.alibaba.fastjson.JSONObject"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="stw" uri="http://stwitinc.com/jsp/tld/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>资讯管理</title>
<stw:import type="jquery" src="/jquery" />
<stw:import type="bootstrap" src="/bootstrap" />
<script type="text/javascript">
	function changeState(state) {
		window.location.href = "${contextPath}/news/news/news_management?tokenId=${param.tokenId}&state=" + state;
	}
	function deleteNews(newsId) {
		showDeleteConfirmDialog({
			title : "删除",
			message : "真的要删除记录吗？",
			confirm : function() {
				$.ajax({
					url : "${contextPath}/news/news/" + newsId + "?tokenId=${param.tokenId}",
					type : "DELETE",
					dataType : "json",
					success : function(data) {
						if (data.error == 0) {
							window.location.href = "${contextPath}/news/news/news_management?tokenId=${param.tokenId}&state=${state}";
						}
					}
				});
			}
		});
	}
	function publishNews(newsId) {
		$.ajax({
			url : "${contextPath}/news/news/" + newsId + "/publish?tokenId=${param.tokenId}",
			type : "PUT",
			dataType : "json",
			success : function(data) {
				if (data.error == 0) {
					window.location.href = "${contextPath}/news/news/news_management?tokenId=${param.tokenId}&state=${state}";
				}
			}
		});
	}
	function showDeleteConfirmDialog(opts) {
		// title,message,confirm
		opts = opts || {};
		$('#deleteConfirmDialog').on('show.bs.modal', function(event) {
			var modal = $(this);
			modal.find('.modal-title').text(opts.title);
			modal.find('.modal-body').text(opts.message);
			modal.find('.btn-danger').click(opts.confirm);
		});
		$('#deleteConfirmDialog').modal("show");
	}
</script>
</head>
<body>
	<ol class="breadcrumb">
		<li><a href="${contextPath}/news/user/logout?tokenId=${param.tokenId}">主页</a></li>
		<li><a href="${contextPath}/news/sys/management?tokenId=${param.tokenId}">功能</a></li>
		<li class="active">资讯管理</li>
	</ol>
	<div style="padding: 10px;">
		<div class="panel panel-default">
			<div class="panel-heading">
				<div class="form-inline">
					<div class="form-group form-group-sm">
						<select class="form-control" onchange="changeState(this.value);">
							<option value="1" ${state==1?'selected':''}>草稿</option>
							<option value="2" ${state==2?'selected':''}>发布</option>
						</select>
					</div>
					<a class="btn btn-primary btn-sm" href="${contextPath}/news/news/news_editor?tokenId=${param.tokenId}">新增</a>
				</div>
			</div>
			<div class="panel-body">
				<table class="table table-striped">
					<thead>
						<tr>
							<th>功能</th>
							<th>编号</th>
							<stw:if test="${state==1}">
								<th>创建时间</th>
								<th>创建者</th>
								<th>修改时间</th>
								<th>修改者</th>
							</stw:if>
							<stw:if test="${state==2}">
								<th>发布时间</th>
								<th>发布者</th>
							</stw:if>
							<th>分类</th>
							<th>作者</th>
							<th>标题图片</th>
							<th>标题</th>
						</tr>
					</thead>
					<tbody>
						<stw:for items="${data.records}" var="rec">
							<tr>
								<td><div class="btn-group btn-group-xs" role="group">
										<a class="btn btn-warning" href="${contextPath}/news/news/${rec.newsId}/news_editor?tokenId=${param.tokenId}">修改</a><a class="btn btn-danger" href="#" onclick="deleteNews('${rec.newsId}')">删除</a>
										<stw:if test="${state==1}">
											<a class="btn btn-success" href="#" onclick="publishNews('${rec.newsId}')">发布</a>
										</stw:if>
										<a class="btn btn-default" href="${contextPath}/news/news/${rec.newsId}/preview?tokenId=${param.tokenId}" target="_blank">预览</a>
									</div></td>
								<td>${rec.newsId}</td>
								<stw:if test="${state==1}">
									<td>${rec.createTime}</td>
									<td>${rec.createUserName}</td>
									<td>${rec.modifyTime}</td>
									<td>${rec.modifyUserName}</td>
								</stw:if>
								<stw:if test="${state==2}">
									<td>${rec.publishTime}</td>
									<td>${rec.publishUserName}</td>
								</stw:if>
								<td>${rec.category eq '1' ? '医护资讯' : (rec.category eq '2' ? '患者资讯' : '通用')}</td>
								<td>${rec.authorName}</td>
								<td align="center"><img class="img-rounded" style="width: 24px; height: 24px;" src="${rec.ossPresignedTitleImageURL}"></td>
								<td>${rec.title}</td>
							</tr>
						</stw:for>
					</tbody>
				</table>
			</div>
		</div>
		<span class="text-info">第${data.pageIndex+1}页，共${data.pageCount}页</span>
		<nav aria-label="..." style="display: inline-block;">
			<ul class="pager">
				<stw:if test="${data.pageIndex>0}">
					<li><a href="${contextPath}/news/news/news_management?tokenId=${param.tokenId}&state=${state}&pageIndex=${data.pageIndex-1}">上一页</a></li>
				</stw:if>
				<stw:if test="${data.pageIndex<data.pageCount-1}">
					<li><a href="${contextPath}/news/news/news_management?tokenId=${param.tokenId}&state=${state}&pageIndex=${data.pageIndex+1}">下一页</a></li>
				</stw:if>
			</ul>
		</nav>
	</div>
	<!-- 删除对话框 -->
	<div class="modal fade" id="deleteConfirmDialog" tabindex="-1" role="dialog" aria-labelledby="deleteConfirmDialogLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title" id="deleteConfirmDialogLabel"></h4>
				</div>
				<div class="modal-body"></div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-danger">删除</button>
				</div>
			</div>
		</div>
	</div>
</body>
</html>